package helper

import (
	"com.jtthink/go_rabbitMQ/add_init"
	"log"
)

func SetNotify(userId string, delay int) (r int) {

	sql := `INSERT INTO user_notify(user_id,updatetime)
VALUES(?,NOW())ON DUPLICATE KEY UPDATE 
notifynum=IF(isdone=1,notifynum,notifynum+1),
isdone=IF(notifynum>=?,1,0),
updatetime=IF(isdone=1,updatetime,NOW())`

	stmt, err := add_init.GetDB().Prepare(sql)

	if err != nil {
		log.Println(err)
		return
	}
	defer stmt.Close()

	//处理引用字段
	ref, err := stmt.Exec(userId, delay)
	if err != nil {
		log.Println(err)
		return
	}

	//受影响的行
	//>0 代表继续发送
	//=0 代表到了多大值  不用发送
	rowsAffect, err := ref.RowsAffected()
	if err != nil {
		log.Println(err)
		return
	}

	return int(rowsAffect)
}
